<template>
	<view class="search-list-container">
		<!-- 头部区域 -->
		<headerSlotVue :propsColor="'#fff'">
			<template v-slot:header>
				<view class="header-content" @click="closePage()">
					<view class="header-left">
						<view class="back-btn">
							<text class="back-icon">←</text>
						</view>
					</view>
					<view class="header-center">
						<text class="page-title">搜索商品</text>
					</view>
					<view class="header-right">
						<view class="header-actions">
							<text class="action-text">{{goodsList.length}}个结果</text>
						</view>
					</view>
				</view>
			</template>
		</headerSlotVue>

		<!-- 搜索区域 -->
		<view class="search-section">
			<view class="search-container">
				<view class="search-icon">
					<text class="cuIcon-search"></text>
				</view>
				<input class="search-input" :adjust-position="false" type="text" placeholder="搜索心仪商品"
					confirm-type="search" v-model="searchVal" @blur="searchProduct" />
				<view class="search-btn" @click="searchProduct" v-if="searchVal.trim()">
					<text class="search-text">搜索</text>
				</view>
			</view>
		</view>

		<!-- 商品列表 -->
		<view class="goods-list-section">
			<view v-if="goodsList.length > 0" class="goods-list">
				<view class="goods-card" v-for="item in goodsList" :key="item._id">
					<view class="goods-content">
						<!-- 商品图片 -->
						<view class="goods-image" @click="goPage(item)">
							<image :src="item.mainImage" mode="aspectFill"></image>
							<view class="image-overlay">
								<text class="overlay-text">查看详情</text>
							</view>
						</view>

						<!-- 商品信息 -->
						<view class="goods-info">
							<view class="goods-header" @click="goPage(item)">
								<text class="goods-title">{{item.name}}</text>
								<text class="goods-desc">{{item.description}}</text>
							</view>

							<view class="goods-footer">
								<view class="price-section" @click="goPage(item)">
									<view class="price-info">
										<text
											class="current-price">￥{{item.displayPrice || item.currentPrice || item.price}}</text>
										<text class="original-price"
											v-if="item.isSpecialOffer && item.originalPrice > (item.displayPrice || item.currentPrice || item.price)">
											￥{{item.originalPrice}}
										</text>
									</view>
									<view class="discount-badge" v-if="item.isSpecialOffer">
										<text class="discount-text">特价</text>
									</view>
								</view>

								<view class="action-section">
									<view class="quantity-control">
										<cc-numbox @change="numChangeClick" :maxNum="item.stock" :isSetMax="true"
											:quantity="cartQuantity"></cc-numbox>
									</view>
									<view class="add-cart-btn" @click="addCartList(item)">
										<text class="cart-icon">🛒</text>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>

			<!-- 空状态 -->
			<view v-else class="empty-state">
				<view class="empty-icon">🔍</view>
				<text class="empty-title">未找到相关商品</text>
				<text class="empty-desc">试试其他关键词或联系客服</text>
			</view>
		</view>
	</view>
</template>

<script>
	import headerSlotVue from '@/components/header-slot.vue';
	import storage from "@/utils/storage.js";
	import config from '@/config/config';
	import {
		getGoodsSearch
	} from '@/api/home.js'
	import {
		AddCart
	} from "@/api/cart.js";
	import {
		errorHandler
	} from '@/utils/errorHandler.js';

	export default {
		components: {
			headerSlotVue,
		},
		data() {
			return {
				config,
				searchVal: '',
				goodsList: [],
				cartQuantity: 1
			}
		},
		onLoad(opt) {
			this.searchVal = opt.searchVal
			this.searchProduct()
		},
		methods: {
			searchProduct() {
				if (!this.searchVal.trim()) {
					uni.showToast({
						title: "请输入搜索关键词",
						icon: "none"
					});
					return;
				}

				uni.showLoading({
					title: '搜索中...',
					mask: true
				});

				getGoodsSearch(this.searchVal).then(res => {
					this.goodsList = res.data.data.list
					uni.hideLoading();
				}).catch(err => {
					uni.hideLoading();
					uni.showToast({
						title: "搜索失败",
						icon: "error"
					});
				})
			},
			//计算单个商品价格
			finalPrice(goodsItem) {
				// 优先使用 currentPrice 字段进行计算
				const basePrice = goodsItem.currentPrice || goodsItem.price;

				if (!goodsItem.isSpecialOffer) {
					return basePrice.toFixed(2); // 普通商品直接显示
				}

				// 特价商品计算逻辑
				const discount = goodsItem.specialOfferInfo.discountRate * 0.1;
				return (basePrice * discount).toFixed(2);
			},
			addCartList(item) {
				// 验证库存
				if (!item.stock || item.stock <= 0) {
					uni.showToast({
						title: '商品库存不足',
						icon: 'none'
					});
					return;
				}

				// 验证购买数量不超过库存
				if (this.cartQuantity > item.stock) {
					uni.showToast({
						title: '购买数量超出库存',
						icon: 'none'
					});
					return;
				}

				AddCart({
					productId: item._id,
					quantity: this.cartQuantity
				}).then(res => {
					if (res.data.code === 200) {
						uni.showToast({
							title: "加购成功",
							icon: "success",
						});
					} else {
						uni.showToast({
							title: res.data.message || "加购失败",
							icon: "none",
						});
					}
				}).catch(err => {
					console.error('加入购物车失败:', err);
					// 使用统一的错误处理
					errorHandler.showApiError(err, '加购失败');
				})
			},
			closePage() {
				uni.navigateBack()
			},
			goPage(item) {
				this.$router.smartNavigate({
					url: '/pages/homePages/sam/samGoodsInfo?id=' + item._id
				})
			},
			numChangeClick(data) {
				this.cartQuantity = data.num
			},
		}
	}
</script>

<style lang="scss" scoped>
	.search-list-container {
		background: linear-gradient(135deg, #f8f9fa 0%, #ffffff 100%);
		min-height: 100vh;
	}

	// 头部样式
	.header-content {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 32rpx;
		width: 100%;

		.header-left {
			.back-btn {
				width: 64rpx;
				height: 64rpx;
				background: rgba(255, 255, 255, 0.9);
				border-radius: 50%;
				display: flex;
				align-items: center;
				justify-content: center;
				box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
				transition: all 0.3s ease;

				&:active {
					transform: scale(0.95);
					background: rgba(255, 255, 255, 1);
				}

				.back-icon {
					font-size: 32rpx;
					color: #333;
					font-weight: 600;
				}
			}
		}

		.header-center {
			flex: 1;
			text-align: center;

			.page-title {
				font-size: 36rpx;
				font-weight: 700;
				color: #333;
			}
		}

		.header-right {
			.header-actions {
				.action-text {
					font-size: 24rpx;
					color: #666;
					background: rgba(255, 255, 255, 0.8);
					padding: 8rpx 16rpx;
					border-radius: 20rpx;
				}
			}
		}
	}

	// 搜索区域
	.search-section {
		padding: 24rpx;
		background: white;
		margin-bottom: 16rpx;

		.search-container {
			display: flex;
			align-items: center;
			background: #f8f9fa;
			border-radius: 24rpx;
			padding: 0 20rpx;
			border: 2rpx solid transparent;
			transition: all 0.3s ease;

			&:focus-within {
				border-color: #FF6B35;
				background: white;
				box-shadow: 0 4rpx 16rpx rgba(255, 107, 53, 0.1);
			}

			.search-icon {
				margin-right: 16rpx;

				text {
					font-size: 32rpx;
					color: #999;
				}
			}

			.search-input {
				flex: 1;
				height: 72rpx;
				font-size: 28rpx;
				color: #333;
				background: transparent;
			}

			.search-btn {
				margin-left: 16rpx;
				padding: 8rpx 16rpx;
				background: #FF6B35;
				border-radius: 16rpx;

				.search-text {
					color: white;
					font-size: 24rpx;
					font-weight: 600;
				}
			}
		}
	}

	// 商品列表区域
	.goods-list-section {
		padding: 0 24rpx;
	}

	.goods-list {
		.goods-card {
			background: white;
			border-radius: 20rpx;
			margin-bottom: 16rpx;
			overflow: hidden;
			box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.08);
			transition: all 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);

			&:active {
				transform: translateY(-2rpx);
				box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.12);
			}

			.goods-content {
				display: flex;
				padding: 24rpx;
			}

			.goods-image {
				width: 200rpx;
				height: 200rpx;
				margin-right: 24rpx;
				border-radius: 16rpx;
				overflow: hidden;
				position: relative;
				flex-shrink: 0;

				image {
					width: 100%;
					height: 100%;
					border-radius: 16rpx;
					transition: all 0.3s ease;
				}

				.image-overlay {
					position: absolute;
					top: 0;
					left: 0;
					right: 0;
					bottom: 0;
					background: rgba(0, 0, 0, 0.5);
					display: flex;
					align-items: center;
					justify-content: center;
					opacity: 0;
					transition: all 0.3s ease;
					border-radius: 16rpx;
				}

				&:active .image-overlay {
					opacity: 1;
				}

				.overlay-text {
					color: white;
					font-size: 24rpx;
					font-weight: 600;
				}
			}

			.goods-info {
				flex: 1;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				min-width: 0;

				.goods-header {
					flex: 1;
					margin-bottom: 16rpx;

					.goods-title {
						font-size: 32rpx;
						font-weight: 700;
						color: #333;
						line-height: 1.4;
						margin-bottom: 12rpx;
						display: block;
						overflow: hidden;
						text-overflow: ellipsis;
						display: -webkit-box;
						-webkit-line-clamp: 2;
						-webkit-box-orient: vertical;
					}

					.goods-desc {
						font-size: 26rpx;
						color: #666;
						line-height: 1.4;
						overflow: hidden;
						text-overflow: ellipsis;
						display: -webkit-box;
						-webkit-line-clamp: 2;
						-webkit-box-orient: vertical;
					}
				}

				.goods-footer {
					display: flex;
					flex-direction: column;
					gap: 16rpx;

					.price-section {
						display: flex;
						align-items: center;
						justify-content: space-between;
						gap: 12rpx;

						.price-info {
							display: flex;
							align-items: center;
							gap: 12rpx;
							flex-wrap: wrap;

							.current-price {
								font-size: 36rpx;
								font-weight: 700;
								color: #FF2E44;
							}

							.original-price {
								font-size: 24rpx;
								color: #999;
								text-decoration: line-through;
							}
						}

						.discount-badge {
							background: linear-gradient(135deg, #FF4D4F 0%, #FF7875 100%);
							border-radius: 12rpx;
							padding: 4rpx 8rpx;
							flex-shrink: 0;

							.discount-text {
								color: white;
								font-size: 20rpx;
								font-weight: 600;
							}
						}
					}

					.action-section {
						display: flex;
						align-items: center;
						justify-content: space-between;
						gap: 16rpx;

						.quantity-control {
							flex: 1;
						}

						.add-cart-btn {
							width: 80rpx;
							height: 64rpx;
							// background: linear-gradient(135deg, #FF6B35 0%, #FE6100 100%);
							border-radius: 32rpx;
							display: flex;
							align-items: center;
							justify-content: center;
							box-shadow: 0 4rpx 16rpx rgba(254, 97, 0, 0.3);
							transition: all 0.3s ease;
							flex-shrink: 0;

							&:active {
								transform: scale(0.9);
								box-shadow: 0 2rpx 8rpx rgba(254, 97, 0, 0.4);
							}

							.cart-icon {
								color: #FF2E44;
								font-size: 28rpx;
							}
						}
					}
				}
			}
		}
	}

	// 空状态
	.empty-state {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 120rpx 32rpx;

		.empty-icon {
			font-size: 120rpx;
			margin-bottom: 32rpx;
			opacity: 0.6;
		}

		.empty-title {
			font-size: 36rpx;
			color: #666;
			font-weight: 600;
			margin-bottom: 16rpx;
		}

		.empty-desc {
			font-size: 28rpx;
			color: #999;
		}
	}

	// 兼容旧样式
	.header_slot {
		padding: 0 32rpx;
		width: 100%;
		font-weight: 500;
		font-size: 36rpx;
		color: #2C2622;
	}

	.shoppingCart_box {
		padding: 12rpx 0;
	}

	.goods_img {
		width: 40%;
		height: 220rpx;
		margin-right: 20rpx;
	}

	.goods_img image {
		border-radius: 20rpx;
	}

	.goods_title {
		font-weight: 700;
		font-size: 32rpx;
		color: #000;
		line-height: 40rpx;
	}

	.goods_pire {
		margin-top: 20rpx;
		font-weight: 700;
		font-size: 32rpx;
		color: #FF2E44;
		line-height: 46rpx;
	}

	.goods_num {
		width: 100%;
		position: absolute;
		left: 0;
		bottom: 0;
	}

	.goods_info_add_afr {
		font-size: 10px;
		width: 30rpx;
		height: 30rpx;
		line-height: 28rpx;
		text-align: center;
		position: absolute;
		top: -20%;
		right: -30%;
		background-color: red;
		border-radius: 50%;
		border: 1px solid #fff;
		color: #fff;
	}
</style>